﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MvcWebRole1.Models;

namespace MvcWebRole1.Controllers
{
    public class StoreController : ApiController
    {
        // GET api/store        
        public IEnumerable<StoreModel> GetAllStores()
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM Store", conn))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<StoreModel> storeModelList = new List<StoreModel>();
                    while (reader.Read())
                    {
                        storeModelList.Add(new StoreModel() { Id = reader.GetInt32(0).ToString(), Name = reader.GetString(1), City = reader.GetString(2), CityImageUrl = reader.GetString(3) });
                        //Console.WriteLine("{0} {1} {2}",
                        //reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                        //ViewBag.Message += @"\n" +  + ": " + reader.GetString(1);
                    }
                    return storeModelList;

                }
                //comments = conn.ExecuteMapperQuery<String>("select * from Comment");
            }
        }

        // GET api/store/5
        public StoreModel Get(int id)
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM Store WHERE Id = "+id, conn))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    StoreModel tempStoreModel = new StoreModel();
                    while (reader.Read())
                    {
                        tempStoreModel.Id = reader.GetInt32(0).ToString();
                        tempStoreModel.Name = reader.GetString(1);
                        tempStoreModel.City = reader.GetString(2);
                        tempStoreModel.CityImageUrl = reader.GetString(3);
                        //Console.WriteLine("{0} {1} {2}",
                        //reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                        //ViewBag.Message += @"\n" +  + ": " + reader.GetString(1);
                    }
                    return tempStoreModel;
                }
                //comments = conn.ExecuteMapperQuery<String>("select * from Comment");
            }
        }

        // POST api/store
        public void Post(string value)
        {
        }

        // PUT api/store/5
        public void Put(int id, string value)
        {
        }

        // DELETE api/store/5
        public void Delete(int id)
        {
        }
    }
}
